#include "SdkNightMonitor.h"

#define DEF_TAG         "Night"

typedef struct
{
    int day_to_night_lum;
    int threshold_day_cnt_0_8;
    int threshold_day_cnt_9;

    int night_to_day_lum;
    int threshold_night_cnt_0_4;
}SSdkAutoDayNightParam;

Com_u8 g_SdkNightMonitor_NeedSwitchMode = DEF_COM_TRUE;
EnSdkNightMode g_SdkNightMonitor_WorkMode = AUTO_NIGHT_MODE;

void Ext_SdkNightMonitor_SwitchMode(Com_u8 flag)
{
    g_SdkNightMonitor_NeedSwitchMode = flag;
}

void Ext_SdkNightMonitor_SetWorkMode(EnSdkNightMode flag)
{
    g_SdkNightMonitor_WorkMode = flag;
}

void SdkNightMonitor_SetIRLedAndIRCut(int mode)
{
    /* mode swtich */
    if (mode == ON_NIGHT_MODE)
    {
        ak_vi_switch_mode(VIDEO_DEV0, VI_MODE_NIGHTTIME);
        Ext_SdkMediaCom_Set_IRcut_IRled(ON_NIGHT_MODE);
    }
    else if (mode == OFF_NIGHT_MODE)
    {
        Ext_SdkMediaCom_Set_IRcut_IRled(OFF_NIGHT_MODE);
        ak_vi_switch_mode(VIDEO_DEV0, VI_MODE_DAY_OUTDOOR);
    }
}

static Com_s32 SdkNightMonitor_GetParam(SSdkAutoDayNightParam *param)
{
    char *p = NULL;
    char buf[200] = {0};

    FILE *fp = fopen("/mnt/config/auto_day_night_param.cgi", "r");
    if (fp == NULL)
    {
        fp = fopen("/usr/ipcam/bak/auto_day_night_param.cgi", "r");
        if (fp == NULL)
        {
            return -1;
        }
    }

    while((fgets(buf, sizeof(buf), fp))!=NULL)
    {
        if((p = strstr(buf, "d_to_n_lum=")) != NULL)
            param->day_to_night_lum = atoi(p + strlen("d_to_n_lum="));
        else if ((p = strstr(buf, "day_cnt_0_8=")) != NULL)
            param->threshold_day_cnt_0_8 = atoi(p + strlen("day_cnt_0_8="));
        else if ((p = strstr(buf, "day_cnt_9=")) != NULL)
            param->threshold_day_cnt_9 = atoi(p + strlen("day_cnt_9="));
        else if ((p = strstr(buf, "n_to_d_lum=")) != NULL)
            param->night_to_day_lum = atoi(p + strlen("n_to_d_lum="));
        else if ((p = strstr(buf, "night_cnt_0_4=")) != NULL)
            param->threshold_night_cnt_0_4 = atoi(p + strlen("night_cnt_0_4="));
        else
        {
            fclose(fp);
            printf("parse auto_day_night_param.cgi fail ...\n");
            return -1;
        }
    }

    fclose(fp);

    return 0;
}

void SdkNightMonitor_SetAutoModeParam(void)
{
    int i = 0;
    struct ak_auto_day_night_threshold threshold;

    if(Ext_InComFile_Access(DEF_SENSOR_INFO_1080P) == DEF_COM_FILE_EXIST)
    {
        threshold.day_to_night_lum = 3968;
        if(Ext_InComFile_Access(DEF_SENSOR_INFO_MIS2008) == DEF_COM_FILE_EXIST)
        {
            threshold.day_to_night_lum = 738;
        }
        threshold.night_to_day_lum = 1563;

        for (i = 0; i < NIGHT_ARRAY_NUM; i++)
        {
            threshold.night_cnt[i] = 12000;
            if(Ext_InComFile_Access(DEF_SENSOR_INFO_MIS2008) == DEF_COM_FILE_EXIST)
            {
                threshold.night_cnt[i] = 450000;
            }
        }

        for (i = 0; i < (DAY_ARRAY_NUM - 1); i++)
        {
            threshold.day_cnt[i] = 650000;
        }
        threshold.day_cnt[9] = 1800000;
    }
    else if(Ext_InComFile_Access(DEF_SENSOR_INFO_720P) == DEF_COM_FILE_EXIST)
    {
        threshold.day_to_night_lum = 3968;
        threshold.night_to_day_lum = 1088;

        for (i = 0; i < NIGHT_ARRAY_NUM; i++)
        {
            threshold.night_cnt[i] = 1000;
        }

        for (i = 0; i < (DAY_ARRAY_NUM - 1); i++)
        {
            threshold.day_cnt[i] = 650000;
        }
        threshold.day_cnt[9] = 1800000;
    }
    else if(Ext_InComFile_Access(DEF_SENSOR_INFO_QXGA) == DEF_COM_FILE_EXIST)
    {
        threshold.day_to_night_lum = 3968;
        threshold.night_to_day_lum = 1000;

        for (i = 0; i < NIGHT_ARRAY_NUM; i++)
        {
            threshold.night_cnt[i] = 14000;
        }

        for (i = 0; i < (DAY_ARRAY_NUM - 1); i++)
        {
            threshold.day_cnt[i] = 650000;
        }
        threshold.day_cnt[9] = 1800000;
    }

    SSdkAutoDayNightParam param = {0};
    if (SdkNightMonitor_GetParam(&param) == 0)
    {
        threshold.day_to_night_lum = param.day_to_night_lum;
        threshold.night_to_day_lum = param.night_to_day_lum;

        for(i = 0;i < DAY_ARRAY_NUM;i++)
        {
            threshold.day_cnt[i] = param.threshold_day_cnt_0_8;
        }
        threshold.day_cnt[9] = param.threshold_day_cnt_9;

        for(i = 0;i < NIGHT_ARRAY_NUM;i++)
        {
            threshold.night_cnt[i] = param.threshold_night_cnt_0_4;
        }
        printf ("threshold.day_to_night_lum= %d !\n", threshold.day_to_night_lum);
        printf ("threshold.night_to_day_lum= %d !\n", threshold.night_to_day_lum);
        printf ("threshold.day_cnt[0]= %d !\n", param.threshold_day_cnt_0_8);
        printf ("threshold.day_cnt[9]= %d !\n", threshold.day_cnt[9]);
        printf ("threshold.night_cnt[0]= %d !\n", param.threshold_night_cnt_0_4);
    }

    if(Ext_InComFile_Access(DEF_SENSOR_INFO_3332) == DEF_COM_FILE_EXIST)
    {
        threshold.night_cnt[1] = 480000;
    }

    threshold.lock_time = 1800000;
    threshold.day2night_sleep_time = 500;
    threshold.night2day_sleep_time = 2000;

    ak_vpss_set_auto_day_night_param(VIDEO_DEV0, &threshold);
}

Com_pvoid SdkNightMonitor_Thread(Com_pvoid arg)
{
    while (Ext_InComFile_Access(DEF_FLAG_SYNC_TIME_OK) == DEF_COM_FILE_NOT_EXIST)
    {
        usleep(100 * 1000);
    }

    int ret = 0;
    int cur_status = -1;
    int pre_status = STATE_DAY;
    SdkNightMonitor_SetAutoModeParam();

    DEF_COM_SET_THREAD_NAME("Night_Monitor",g_ComThread_Num);
    while(g_ComThread_Running)
    {
        if ((g_SdkNightMonitor_WorkMode == ON_NIGHT_MODE) && (g_SdkNightMonitor_NeedSwitchMode == DEF_COM_TRUE))
        {
            SdkNightMonitor_SetIRLedAndIRCut(ON_NIGHT_MODE);
            g_SdkNightMonitor_NeedSwitchMode = DEF_COM_FALSE;
        }
        else if ((g_SdkNightMonitor_WorkMode == OFF_NIGHT_MODE) && (g_SdkNightMonitor_NeedSwitchMode == DEF_COM_TRUE))
        {
            SdkNightMonitor_SetIRLedAndIRCut(OFF_NIGHT_MODE);
            g_SdkNightMonitor_NeedSwitchMode = DEF_COM_FALSE;
        }
        else if(g_SdkNightMonitor_WorkMode == AUTO_NIGHT_MODE)
        {
            /* Force switch back to day mode */
            if (g_SdkNightMonitor_NeedSwitchMode == DEF_COM_TRUE)
            {
                pre_status = STATE_DAY;
                SdkNightMonitor_SetIRLedAndIRCut(OFF_NIGHT_MODE);
                g_SdkNightMonitor_NeedSwitchMode = DEF_COM_FALSE;
            }

            /* check day night mode */
            ret = ak_vpss_get_auto_day_night_level(VIDEO_DEV0,pre_status,&cur_status);
            if (ret)
            {
                ak_print_error_ex(MODULE_ID_APP, "ak_vpss_isp_get_input_level failed.\n");
                continue;
            }

            if (pre_status != cur_status)
            {
                if (STATE_DAY == cur_status)
                {
                    PRT_INFO(DEF_TAG,"set day mode\n");
                    SdkNightMonitor_SetIRLedAndIRCut(OFF_NIGHT_MODE);
                    pre_status = STATE_DAY;
                }
                else
                {
                    PRT_INFO(DEF_TAG, "set night mode\n");
                    SdkNightMonitor_SetIRLedAndIRCut(ON_NIGHT_MODE);
                    pre_status = STATE_NIGHT;
                }
            }
        }
        usleep(200 * 1000);
    }

    DEF_COM_EXIT_THREAD(g_ComThread_Num);
}

void Ext_SdkNightMonitor_Init(void)
{
    pthread_t id;

    pthread_create(&id, NULL, SdkNightMonitor_Thread,NULL);
    pthread_detach(id);
}
